<template>
    <div>
        <!--<transition :name="'vux-pop-' + (direction === 'forward' ? 'in' : 'out')">-->
        <keep-alive>
            <router-view class="router-view" v-wechat-title="$route.meta.title"></router-view>
        </keep-alive>
        <!--</transition>-->
        <!--<div v-transfer-dom>-->
        <!-- <loading v-model="isLoading" text="加载中..."></loading> -->
        <!--<Loadingx v-if="true"></Loadingx>-->
        <toast v-model="isPositionValue" type="text" :time="2000" is-show-mask :text="isPositionValueText" position="top"></toast>
        <toast v-model="isGetLocation" type="text" :time="3000" is-show-mask :text="isGetLocationText" position="top"></toast>
        <!--</div>-->
    </div>
</template>

<script>
    import {
        ButtonTab,
        ButtonTabItem,
        ViewBox,
        XHeader,
        Tabbar,
        TabbarItem,
        Loading,
        TransferDomDirective as TransferDom,
        Toast
    } from 'vux'
    import {mapState, mapActions} from 'vuex'
    import {TestLoginService, GetWeixinConfig} from  './services/index'

    import axios from 'axios';

    export default {
        components: {Loading,Toast},
        watch: {},
        data(){
            return {
                d: '加载中...',
                isGetLocation:false,
                isGetLocationText:'请打开手机GPS定位功能'
            }
        },
        computed: {
            ...mapState({
                route: state => state.route,
                path: state => state.route.path,
                deviceready: state => state.app.deviceready,
                demoTop: state => state.vux.demoScrollTop,
                isLoading: state => state.vux.isLoading,
                direction: state => state.vux.direction,
                isPositionValue:state => state.vux.isPositionValue,
                isPositionValueText:state => state.vux.isPositionValueText,
            })          
        },        
        created(){ 
            axios.post('http://localhost:8002/api/weixin/login/testapi?openid=okH0xw15eUBeSFe4XduVJiTsp37Y').then((res)=>{
                console.log(JSON.stringify(res))
                axios.post('http://localhost:8002/api/weixin/js/config?location=' +  location.href.split('#')[0]).then((res) => {
                    wx.config({
                        debug:false,
                        appId: res.data.data.appId,
                        timestamp: res.data.data.timestamp,
                        nonceStr: res.data.data.nonceStr,
                        signature: res.data.data.signature,
                        jsApiList: [
                            'getLocation',
                            'checkJsApi',
                            'chooseWXPay',
                        ]
                    });
                    wx.ready( () => {
                        wx.getLocation({
                            type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                            success: (res) => {
                                let latitude = res.latitude;//纬度
                                let longitude = res.longitude;//经度

                                const map = new BMap.Map("allmap")
                                const myGeo = new BMap.Geocoder();
                                const geolocation = new BMap.Geolocation()
                                const pt = new BMap.Point(res.longitude, res.latitude)

                                myGeo.getLocation(pt,  (rs) => {
                                    const addComp = rs.addressComponents;
                                    const detailInfo = addComp.province + "" + addComp.city + "" + addComp.district + "" + addComp.street + "" + addComp.streetNumber;
                                    const city = addComp.city.substring(0,addComp.city.length - 1);
                                    const nearStore = {
                                        cityName: city,
                                        longitude: longitude,
                                        latitude: latitude,
                                    }
                                    this.updateNearStoreObj(nearStore);
                                })
                            },
                            fail:(fail) => {
                                //请打开手机GPS定位功能
                                this.isGetLocation = true;
                                const nearStore = {
                                    cityName: '广州',
                                    longitude: 113.264435,
                                    latitude: 23.129163 ,
                                }
                                this.updateNearStoreObj(nearStore);
                            }
                        });
                    });
                }).catch(error=>{
                    console.log("获取配置失败："+JSON.stringify(error));
                    this.updateIsPositionValue(true);
                    this.updateIsPositionValueText("获取配置失败，请和相关人员联系");
                })

            })      

        },
        methods:{
            ...mapActions([
                'updateNearStoreObj',
                'updateIsPositionValue',
                'updateIsPositionValueText'
            ]),
        },
        watch : {
            // isPositionValueText : function(newVal){
            //     if(newVal != '' && newVal == "网络异常"){
            //         window.location.reload();
            //     }
            // }
        }
    }
</script>

<style lang="less" rel="stylesheet/less">
    .weui-toast__content {
        font-size: 14px;
        padding-top: 1rem;
    }
    .xs-plugin-pullup-container{
        line-height: 40px;
    }

    @import "./assets/css/1px";
    @import "./assets/css/vue-router-transition.css";
    @import "./assets/css/common.css";
    // @import "./assets/css/loading";
</style>
